#include "udpserver.h"
#include "u_thread.h"
#include "udpunit.h"
#include "../util/lock.h"

UDPServer::UDPServer()
{
    m_thread = NULL;
}

UDPServer::~UDPServer()
{
	func_lock s_lock;
	if( m_thread != NULL)
	{
		m_thread->StopThread();
		delete m_thread;
	}
}

bool UDPServer::init(uint16 d_port,UDPSINK* udpsink)
{
	func_lock s_lock;
	if(m_udp.init(d_port) == false)
		return false;
     
 	 m_thread = new uthread(udpsink,&m_udp);

	 return m_thread == NULL;
	
}

bool UDPServer::sendbuf(uint32 ip, uint16 port, char* buf, uint32 buf_size)
{
	func_lock s_lock;
	char sendbuf[1500];
	memset(sendbuf,0,1500);
	if(LoadBuffer(buf,buf_size,sendbuf) == false)
		return false;

	return m_udp.sendbuf(ip,port,sendbuf,buf_size + 5);
}

uint16 UDPServer::get_port()
{
	return m_port;
}